package com.sprouting.ops.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.*;
import com.sprouting.ops.model.bo.OpsProductDependBo;
import com.sprouting.ops.model.qo.OpsProductDependQo;
import com.sprouting.ops.model.vo.OpsAppVo;
import com.sprouting.ops.model.vo.OpsComponentVo;
import com.sprouting.ops.model.vo.OpsProductDependVo;
import com.sprouting.ops.model.vo.OpsProductVo;
import com.sprouting.ops.service.OpsAppService;
import com.sprouting.ops.service.OpsComponentService;
import com.sprouting.ops.service.OpsProductDependService;
import com.sprouting.ops.service.OpsProductService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 产品依赖表(OpsProductDepend)() 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-06-01 15:38:29
 */
@Slf4j
@RestController
@RequestMapping("opsProductDepend")
public class OpsProductDependController {

    @Resource
    private OpsProductDependService opsProductDependService;
    @Resource
    private OpsAppService opsAppService;
    @Resource
    private OpsComponentService opsComponentService;
    @Resource
    private OpsProductService opsProductService;


    /**
     * 分页查询对应的依赖
     * @param query 查询参数
     * @return
     */
    @PostMapping("getListPage")
    public ResultJson getListPage(@Validated OpsProductDependQo query){
        if (query == null){
            query = new OpsProductDependQo();
        }
        Page page = query.getPage();
        Page<OpsProductDependVo> listPage = opsProductDependService.getListPage(page, query);
        List<OpsProductDependVo> list = listPage.getRecords();
        if (CollUtil.isEmpty(list)){
            return ResultJson.success(listPage);
        }

        List<String> bindList = list.stream().map(OpsProductDependVo::getBindId).collect(Collectors.toList());
        // 这里其实不需要查询多次，但这里为了省事就直接查询三次了，实际上因为有type进行分类，不会出现三个同时查询的情况
        List<OpsAppVo> appList = opsAppService.getByIdList(bindList);
        List<OpsComponentVo> componentList = opsComponentService.getByIdList(bindList);
        List<OpsProductVo> productList = opsProductService.getByIdList(bindList);

        // 转为map， 对象id -> 应用、组件、产品
        Map<String, OpsAppVo> appMap = EntityTool.toMap(appList, OpsApp::getId);
        Map<String, OpsComponentVo> componentMap = EntityTool.toMap(componentList, OpsComponent::getId);
        Map<String, OpsProductVo> productMap = EntityTool.toMap(productList, OpsProduct::getId);

        for (OpsProductDependVo dependVo : list) {
            dependVo.setOpsProduct(productMap.get(dependVo.getBindId()));
            dependVo.setOpsApp(appMap.get(dependVo.getBindId()));
            dependVo.setOpsComponent(componentMap.get(dependVo.getBindId()));
        }

        return ResultJson.success(listPage);
    }

    /**
     * 删除产品依赖表
     * @param id 产品依赖表id
     * @return
     */
    @PostMapping("delOpsProductDepend")
    @IdempotentSingle
    public ResultJson delOpsProductDepend(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        opsProductDependService.removeById(id);
        return ResultJson.success();
    }

    /**
     * 更新产品依赖表
     * @param bo 修改参数
     * @return
     */
    @PostMapping("updOpsProductDepend")
    @IdempotentSingle
    public ResultJson updOpsProductDepend(@Validated(UpdGroup.class) OpsProductDepend bo){
        EntityTool.updBase(bo);
        opsProductDependService.updateById(bo);
        return ResultJson.success(bo);
    }

    /**
     * 保存产品依赖表
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsProductDepend")
    @IdempotentSingle
    public ResultJson saveOpsProductDepend(@Validated(AddGroup.class) OpsProductDependBo bo){
        String msg = checkOpsProductDepend(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        for (OpsProductDepend depend : bo.getDependList()) {
            depend.setId(IdTool.getId());
            depend.setProductId(bo.getProductId());
            EntityTool.saveBase(depend);
        }

        opsProductDependService.saveBatch(bo.getDependList());
        return ResultJson.success(bo);
    }


    /**
     * 产品依赖表数据校验
     * @param bo 产品依赖表 校验数据
     * @return 非null则有错误
     */
    private String checkOpsProductDepend(OpsProductDependBo bo){
        for (OpsProductDepend opsProductDepend : bo.getDependList()) {
            if (opsProductDepend.getNecessary() == null){
                opsProductDepend.setNecessary(ConstantBase.YesOrNo.NO);
            }
        }
        return null;
    }

}
